<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let info = { name: 'why', age: 18 }
    // const obj = info // 强引用
    let obj = new WeakRef(info) // 弱引用
    let obj2 = new WeakRef(info) // 弱引用

    const finalRegistry = new FinalizationRegistry((key) => {
      console.log('被回收了', key)
    })
    finalRegistry.register(info, 'info')

    setTimeout(() => {
      info = null
    }, 2000)

    setTimeout(() => {
      console.log(obj.deref().name, obj.deref().age)
    }, 15000)

  </script>
</body>

</html>